package org.mspring.mlog.web.freemarker.directive;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import java.io.IOException;
import java.util.Map;
import org.mspring.platform.gavatar.GravatarUtils;
import org.mspring.platform.utils.StringUtils;
import org.mspring.platform.web.freemarker.DirectiveUtils;
import org.mspring.platform.web.freemarker.directive.AbstractDirectiveModel;

/**
 * 生成Gravatar图像地址
 *
 * @author Gao Youbo
 * @since 2013-3-4
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class GravatarDirectiveModel extends AbstractDirectiveModel {

    @Override
    public void exec(Environment env, Map params, TemplateModel[] model, TemplateDirectiveBody body) throws TemplateException, IOException {
        String email = DirectiveUtils.getString("email", params);
        if (StringUtils.isNotBlank(email)) {
            String url = GravatarUtils.getGravatarImage(email);
            env.getOut().append(url);
        }
    }

}
